import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({
   easing: "ease-in-out",
   speed: 300,
   trickeSpeed: 300,
})
const instance=axios.create({
    baseURL:process.env.VUE_APP_URL,
    timeout:1000
})
/**
 * 配置请求拦截器
 */
instance.interceptors.request.use(config=>{
    NProgress.start()
    //从本地存储中获取到token
    const info=localStorage.getItem("rbac-login")
    if(info){
       const token=JSON.parse(info).login.user.token
       if(token){
        config.headers['Authorization']=token
       }
    }
    return config
},err=>{
    return Promise.reject(err)
})
/**
 * 配置响应拦截器
 */
instance.interceptors.response.use(response=>{
   NProgress.done()
   return response.data
    if(err.response){
        switch(err.reponse.status){
            case 401:
                break;
            case 404:
                break;
            case 500:
                break;
        }
    }
    return Promise.reject(err)
})
/**
 * 封装get请求
 * @param {*} url 
 * @param {*} params 
 * @param {*} headers 
 * @returns 
 */
export const get=(url,params,headers)=>{
    return instance.get(url,{
        params,
        headers
    })
}
/**
 * 封装POST请求
 * @param {*} url 
 * @param {*} data 
 * @param {*} headers 
 * @returns 
 */
export const post=(url,data,headers)=>{
    return instance.post(url,data,{
        headers
    })
}
export default instance